﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DataSet ds = getDataSet();
        int results = ds.Tables[0].Rows.Count;
        if (results == 0)
        {
            //output message "no results founds"
            lblNoResults.Visible = true;
            gvSearchResults.Controls.Clear();

        }
        else if (results == 1)
        {
            //Send to details page with property id
            Response.Redirect("Property_Info/Property_Details.aspx?pid=" + ds.Tables[0].Rows[0][0]);
        }
        else
        {
            //fill gridview with possible results
            bind();
            lblNoResults.Visible = false;
        }
    }
    protected void gvSearchResults_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("Property_Info/Property_Details.aspx?pid=" + gvSearchResults.SelectedDataKey.Value.ToString());
    }
    protected void gvSearchResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSearchResults.PageIndex = e.NewPageIndex;
        bind();
    }
    private DataSet getDataSet()
    {
        Property_Data pd = new Property_Data();
        DataSet dsSearchResults = pd.Search(txtSearchCriteria.Text);
        return dsSearchResults;
    }

    private void bind()
    {
        DataSet ds = getDataSet();
        gvSearchResults.DataSource = ds.Tables[0];
        gvSearchResults.DataBind();
    }
}
